package com.tiyo.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiyo.common.core.domain.Ztree;
import com.tiyo.common.core.page.PageDomain;
import com.tiyo.common.core.page.TableSupport;
import com.tiyo.common.utils.StringUtils;
import com.tiyo.system.mapper.SysDeptMapper;
import com.tiyo.system.mapper.domain.SysDept;
import com.tiyo.system.service.DeptService;
@Service
public class DeptServiceImpl implements DeptService {
	@Autowired
	SysDeptMapper sysDeptMapper;
	@Override
	public List<Ztree> selectDeptTree(SysDept dept) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<SysDept>();
		queryWrapper.eq("del_flag", 0);
		if(StringUtils.isNotEmpty(dept.getDeptName())) {
			queryWrapper.eq("dept_name", dept.getDeptName());
		}
		if(StringUtils.isNotEmpty(dept.getParentId())) {
			queryWrapper.eq("parent_id", dept.getParentId());
		}
		return null;
	}
	@Override
	public IPage<SysDept> page(SysDept dept) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<SysDept>();
		if(StringUtils.isNotEmpty(dept.getDeptId())) {
			queryWrapper.eq("dept_id", dept.getDeptId());
		}
		if(StringUtils.isNotEmpty(dept.getParentId())) {
			queryWrapper.eq("parent_id", dept.getParentId());
		}
		PageDomain pageDomian= TableSupport.buildPageRequest();
		Page<SysDept> page = new Page<SysDept>(pageDomian.getCurrent(), pageDomian.getSize());
		queryWrapper.orderBy(pageDomian.isCondition(), pageDomian.isAsc(), pageDomian.getOrderByXiaHuaXianColumn());
		IPage<SysDept> ipage = sysDeptMapper.selectPage(page, queryWrapper);
		for (SysDept sysDept : ipage.getRecords()) {
			sysDept.setIsTreeLeaf("1");
		}
		return ipage;
	}
	@Override
	public List<SysDept> list(SysDept dept) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<SysDept>();
		if(StringUtils.isNotEmpty(dept.getDeptId())) {
			queryWrapper.eq("dept_id", dept.getDeptId());
		}
		if(StringUtils.isNotEmpty(dept.getParentId())) {
			queryWrapper.eq("parent_id", dept.getParentId());
		}
		List<SysDept> list =sysDeptMapper.selectList(queryWrapper);
		for (SysDept sysDept : list) {
			sysDept.setIsTreeLeaf("1");
		}
		return list;
	}

}
